package com.ita.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ita.dao.UserDao;
import com.ita.pojo.User;
import com.ita.util.DBUtil;

public class UserDaoImpl implements UserDao {

	@Override
	public int addUser(User user) {
		String sql = "insert into users values(seq01.nextval,?,?,?,?,?)";
		Connection con = null;
		PreparedStatement pst = null;
		int result = 0;
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, user.getUsername());
			pst.setDouble(2, user.getSalary());
			pst.setDate(3, new java.sql.Date(user.getBirth().getTime()));
			pst.setString(4, user.getSex());
			pst.setBytes(5, user.getAvatar());
			result = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return result;
	}

	@Override
	public int checkUser(String username) {

		String sql = "SELECT count(*) count FROM users where USERNAME=?";
		Connection con = null;
		PreparedStatement pst = null;
		int result = 0;
		ResultSet rset = null;
		try {
			con = DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, username);
			rset = pst.executeQuery();
			if (rset.next()) {
				result = rset.getInt("count");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, rset);
		}
		return result;
	}
	
	@Override
	public List<User> findAll() {
		List<User> list=new ArrayList<User>();
		String sql = "select * from users";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			rs = pst.executeQuery();
			while (rs.next()) {
				User u=new User();
				u.setUsername(rs.getString("username"));
				u.setAvatar(rs.getBytes("avatar"));
				u.setSalary(rs.getDouble("salary"));
				u.setSex(rs.getString("sex"));
				Date birth = new Date(rs.getDate("birth").getTime());
				u.setBirth(birth);
				list.add(u);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, rs);
		}
		return list;
	}
	
	public User loadCustomer(String username) {
		User u=null;
		String sql = "select * from users where username=?";
		Connection con=null;
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			con=DBUtil.createConnection();
			pst = con.prepareStatement(sql);
			pst.setString(1, username);
			rs = pst.executeQuery();
			if (rs.next()) {
				u=new User();
				u.setUsername(rs.getString("username"));
				u.setAvatar(rs.getBytes("avatar"));
				u.setSalary(rs.getDouble("salary"));
				u.setSex(rs.getString("sex"));
				Date birth = new Date(rs.getDate("birth").getTime());
				u.setBirth(birth);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return u;
	}


	public static void main(String[] args) {
		UserDao userDao = new UserDaoImpl();
		// User user=new User();
		// user.setUsername("123");
		// user.setBirth(new Date());
		// user.setSalary(33.3);
		// user.setSex("男");
		// byte [] buff=new byte[521];
		// user.setAvatar(buff);
		// userDao.addUser(user);
		System.out.println(userDao.checkUser("123"));
		System.out.println(userDao.checkUser("3"));
		System.out.println(userDao.findAll().size());
		
	}
}
